package com.bugbuff.dubbo.server.controller;


import cn.hutool.core.util.ObjectUtil;
import com.bugbuff.common.pojo.PageResult;
import com.bugbuff.common.vo.ErrorResult;
import com.bugbuff.dubbo.server.service.LogsService;
import com.bugbuff.dubbo.server.vo.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("management/manage")
public class LogsController {

    @Autowired
    private LogsService logsService;

    /**
     * 日志管理
     * @param pagesize 页尺寸
     * @param page 页数
     * @param sortProp 排序字段
     * @param sortOrder ascending 升序 descending 降序
     * @param type 类型 01 用户登录
     * @param uid 用户id
     * @return
     */
    @GetMapping("logs")
    public ResponseEntity<Object> logs(@RequestParam(value = "pagesize" ,required = false,defaultValue = "10") Integer pagesize,//页尺寸
                                @RequestParam(value = "page" ,required = false,defaultValue = "1") Integer page,//页数
                               @RequestParam(value = "sortProp" ,required = true) String sortProp,//排序字段
                               @RequestParam(value = "sortOrder" ,required = true) String sortOrder,//ascending 升序 descending 降序
                               @RequestParam(value = "type" ,required = false , defaultValue = "01") String type,//类型 01 用户登录
                               @RequestParam(value = "uid" ,required = true) Long uid){//用户id
        PageResult pageResult = logsService.logs(page,pagesize,sortProp,sortOrder,type,uid);
        if (ObjectUtil.isNotEmpty(pageResult)){
            return ResponseEntity.ok(pageResult);
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("000009").errMessage("获取日志记录失败！！").build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }
}
